package jea.machine.transverseprofileviewer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.LineBorder;

import jea.machine.db.DBConnect;
import jea.machine.db.VacuumProfile;

public class FrameProfileViewerByType extends JInternalFrame implements ActionListener
{
	private static final long serialVersionUID = 1L;
	protected DBConnect db;
	protected JPanel panelMain, panelControls;
	protected PanelProfileViewerByType panelProfile;
	protected JScrollPane mainScrollPane;
	protected JComboBox comboProfiles;
	protected JCheckBox checkStackProfiles;
	
	public FrameProfileViewerByType()
	{
	    	this.setClosable(true);
	    	this.setIconifiable(true);
	    	this.setMaximizable(false);
	    	//this.setResizable(true);
	    	this.setTitle("Profile Viewer by Vacuum Chamber Type");
		    this.setBorder(new LineBorder(Color.DARK_GRAY,2));
		    this.setLocation(100, 100);
		    this.setSize(new Dimension(430,440));
		    this.setVisible(true);
		    
		    initPanels();
	  }
	  private void initPanels()
	  {
	    	this.setLayout(new BorderLayout());
	    	panelMain = new JPanel(new BorderLayout());
            panelMain.setPreferredSize(new Dimension(400,400));
			mainScrollPane = new JScrollPane(panelMain);
			this.add(mainScrollPane);
	    	
            panelProfile = new PanelProfileViewerByType(this);
            panelProfile.setBackground(Color.LIGHT_GRAY);

	    	// recupere dans la DB
	    	db = new DBConnect(panelProfile);
	    	db.connect();
	    	
	    	Vector<String> profilesShortNames = db.getShortNames();
	    	profilesShortNames.add(0, "");
	    	//db.close();
	    	
	    	panelControls = new JPanel(new FlowLayout());
	    	panelControls.setPreferredSize(new Dimension(400,35));
	    	
	    	Object[] listeProfiles = profilesShortNames.toArray();
	    	
	    	 comboProfiles = new JComboBox(listeProfiles);
	    	 comboProfiles.addActionListener(this);
             panelControls.add(comboProfiles);
             
             checkStackProfiles = new JCheckBox("Stack Profiles");
             panelControls.add(checkStackProfiles);
                        
            panelMain.add(panelControls, BorderLayout.NORTH);
            panelMain.add(panelProfile, BorderLayout.CENTER);

	  }
	
	public boolean stackProfiles()
	{
		return checkStackProfiles.isSelected();
	}
	  
	@Override
	public void actionPerformed(ActionEvent e)
	{
		JComboBox cb = (JComboBox)e.getSource();
		if ( cb.getSelectedIndex() == 0 ) return; // ne fait rien avec la selection vide
        String profileName = (String)cb.getSelectedItem();
        System.out.println("Profile to view: "+profileName);
        VacuumProfile vp = db.getProfileByShortName(profileName);
        this.setTitle(profileName+"-"+vp.getProfileDescription());
	}	
	   
	
}
